package com.suncam.suncam2.controller;

import io.socket.client.Ack;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URISyntaxException;


/**
 * @author LZM
 * @version 1.0
 * @Date 2022/1/14
 * @Description 测试socket连接
 */
@RestController
public class SocketDemo {

    private static final Logger log = LoggerFactory.getLogger(SocketDemo.class);

    @GetMapping("send")
    public void testSend() throws URISyntaxException {
        log.debug("正在运行....");
        Socket socket = IO.socket("https://10.0.1.30:9001");
//        Socket socket = IO.socket("https://10.0.1.30/");
        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
//                socket.emit("foo", "hi");
//                socket.emit("chat message", "from java");
//                socket.emit("send", "from java");
//                socket.emit("testAll", "from java");
                socket.emit("test", "from java",new Ack(){

                    @Override
                    public void call(Object... args) {
                        if (args != null && args.length > 0) {
                            log.info("回调： {}",args[0]);
                        }

                    }
                });
                log.info("java 已发送");
                log.info("socketId:{}",socket.id());
                log.info("connected: {}",socket.connected());
//                socket.disconnect();
            }

        }).on("getInfo", new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                System.out.println(args.length);
                if (args != null && args.length > 0) {
                    log.info("接收：{}",args[0]);
                }

//                log.info("接收：{}",args[1]);
//                log.info("接收：{}",args[2]);

            }

        }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                log.info("disconnect");
            }

        });

        socket.connect();


    }

}
